/**
 * Created by yangxianming on 2019/10/22.
 */
//
//接口定义 都是以api开头 所需参数都通过方法传入
//
//通过上传接口，想服务器发起解压请求
function apiUnzip(fileName, version){
    var data = {
        "action":"unzip",
        "fileName":fileName,
        "version":version
    };

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "1"){
                    apiGetList(drawGetList);
                }
                if (r["apiStatus"] == "0") {
                    alert(r["errorMsg"])
                }
            }
        }
    };
    xmlHttp1.open("POST", "/designs/designsDispense", true);
    xmlHttp1.send(JSON.stringify(data));
}

//拿到设计稿列表
function apiGetList(dofun){
    var data = {
        "action":"getList",
        "createtime": document.getElementById("year").value
    };

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "1"){
                    dofun(r["result"]);
                }
                if (r["apiStatus"] == "0") {
                    alert(r["errorMsg"])
                }
            }
        }
    };
    xmlHttp1.open("POST", "/designs/designsDispense", true);
    xmlHttp1.send(JSON.stringify(data));
}
//拿到详细地址并在新页面打开
function apiGetDetailPath(name){
    var data = {
        "action":"getDetailPath",
        "name": name
    };

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "1"){
                    window.open(r["result"]);
                }
                if (r["apiStatus"] == "0") {
                    alert(r["errorMsg"])
                }
            }
        }
    };
    xmlHttp1.open("POST", "/designs/designsDispense", true);
    xmlHttp1.send(JSON.stringify(data));
}


//上传设计稿，完毕后出发解压接口
function apiUploadFile(){
    //var projectSelect = document.getElementById("project");
    //var selectIndex = projectSelect.selectedIndex;
    //var projectValue = projectSelect.options[selectIndex].text;
    var projectValue = "koufu";
    var version = document.getElementById("version").value.trim();

    var fi = document.getElementById('file').files[0];
    var fileName = document.getElementById('file').value;
    var upload = document.getElementById("upload");
    var action = "action=design";

    if(version == ""){
        alert("需要一个版本号呀！");
        return;
    }
    if(fi == undefined){
        alert("需要选择上传的设计稿");
        return;
    }
    var fileEnd = fileName.substring(fileName.length-3, fileName.length);
    if(fileEnd != "zip"){
        alert("只能zip格式");
        return;
    }

    //不要猛点
    upload.setAttribute("disabled", "");
    upload.style.color = "red";
    var form = new FormData();
    form.append("file", fi);

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "1"){
                    var full = fileName + r["result"];
                    apiUnzip(full, version);
                }
                if (r["apiStatus"] == "0") {
                    alert(r["errorMsg"])
                }
            }
        }
    };
    xmlHttp1.open("POST", "../uploadFile?" + action + "&projectName=" + projectValue + "&version=" + version, true);
    xmlHttp1.send(form);

    //loadXMLDoc("post", "../uploadFile?" + action + "&projectName=" + projectValue + "&version=" + version, form, null);
    controlTopDiv('upLoadFile', 'close');
    setTimeout(
        function(){
            upload.removeAttribute("disabled");
            upload.style.color = "black";
        }, 5000);
}

//
//页面渲染 都是以draw开头
//
//列出设计稿列表
function drawGetList(data){
    var myUl = document.getElementById("designsList").getElementsByTagName("ul")[0];
    myUl.innerText = "";
    for(var i = 0; i < data.length; i++){
        var myLi = document.createElement("li");
        var mya = document.createElement("a");
        //mya.setAttribute("href", data[i][1]);
        mya.onclick = function(){apiGetDetailPath(this.getAttribute("dir"))};
        mya.setAttribute("target", "_blank");
        mya.setAttribute("dir", data[i][1]);
        mya.innerText = data[i][0];
        myLi.appendChild(mya);
        myUl.appendChild(myLi);
    }
}


//上传设计稿弹出框
//上传文件弹出层 ob参数一般是this触发这个悬浮层的对象，将更具ob相对位置定位悬浮层位置


//
//页面调用 数据获取，接口层与渲染层的连接
//
function initializeData(){
    //查询历史档案页面 填充当前年份
    var da = new Date();
    document.getElementById("year").value = da.getFullYear();
    //拿到列表
    setTimeout(
        function(){apiGetList(drawGetList)},
        500
    );
}
//弹出层控制
function controlTopDiv(whichWindow, mySwitch, ob){
    var topDiv = document.getElementById(whichWindow);
    if (mySwitch == "open") {
        var left = ob.offsetLeft;
        var top = ob.offsetTop + ob.offsetHeight + 10;
        //显示页面
        topDiv.style.marginTop = document.documentElement.scrollTop + "px";
        topDiv.style.display = "block";
        //设置位置
        var topDiv1001 = topDiv.getElementsByClassName("topDiv1001")[0];
        topDiv1001.style.marginLeft = left + "px";
        topDiv1001.style.marginTop = top + "px";
    }

    if (mySwitch == "close") topDiv.style.display = "none";
}




function projectSelectOnclick(){

}